package com.example.mark.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mark.entity.Topic;
import com.example.mark.vo.TopicDetailVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TopicMapper extends BaseMapper<Topic> {
    
    @Select("SELECT * FROM topic WHERE topic_id = #{topicId}")
    Topic selectByTopicId(int topicId);
    
    @Delete("DELETE FROM topic WHERE topic_id = #{topicId}")
    int deleteByTopicId(int topicId);
    
    @Update("UPDATE topic SET title = #{title}, topic_context = #{topicContext}, " +
            "verify = #{verify}, reply_number = #{replyNumber}, school = #{school}, " +
            "tag = #{tag}, topic_photo = #{topicPhoto} WHERE topic_id = #{topicId}")
    int updateByTopicId(Topic topic);
    
    @Select("SELECT " +
            "t.topic_id as topicId, " +
            "t.topic_user_id as topicUserId, " +
            "t.title, " +
            "t.topic_context as topicContext, " +
            "t.createtime, " +
            "t.verify, " +
            "t.reply_number as replyNumber, " +
            "t.school, " +
            "t.tag, " +
            "t.topic_photo as topicPhoto, " +
            "u.photo_url as userPhoto " +
            "FROM topic t " +
            "LEFT JOIN user_data u " +
            "ON t.topic_user_id = u.user_id " +
            "WHERE t.topic_id = #{topicId}")
    TopicDetailVO getTopicDetail(int topicId);
}
